import time

import redis
from config import REDIS_HOST, REDIS_DATABASE, REDIS_PORT, REDIS_PASSWORD
from utils.guid_util import GUIDUtil


class RedisWrapper(object):
    """
    关于Redis的操作
    """
    pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DATABASE,
                                password=REDIS_PASSWORD)
    r = redis.StrictRedis(connection_pool=pool)

    @staticmethod
    def set(key, value, ex=None):
        """
        设置 key 对应的值为 string 类型的 value
        :param key:
        :param value:
        :param ex:
        :return:
        """
        return RedisWrapper.r.set(key, value, ex)

    @staticmethod
    def expire(name, time):
        return RedisWrapper.r.expire(name, time)

    @staticmethod
    def setnx(key, value):
        """
        设置 key 对应的值为 string 类型的 value。如果 key 已经存在,返回 0,nx 是 not exist 的意思
        :param key:
        :param value:
        :return:
        """
        return RedisWrapper.r.setnx(key, value)

    @staticmethod
    def delete(key):
        return RedisWrapper.r.delete(key)

    @staticmethod
    def setex(key, time, value):
        """
        设置 key 对应的值为 string 类型的 value,并指定此键值对应的有效期
        :param key:
        :param time:
        :param value:
        :return:
        """
        return RedisWrapper.r.setex(key, time, value)

    @staticmethod
    def setrange(key, num, value):
        """
        设置指定 key 的 value 值的子字符串
        setrange name 8 gmail.com，其中的 8 是指从下标为 8(包含 8)的字符开始替换
        :param key:
        :param num:
        :param value:
        :return:
        """
        return RedisWrapper.r.setrange(key, num, value)

    @staticmethod
    def getrange(key, start, end):
        """
        获取指定 key 的 value 值的子字符串
        :param key:
        :param start:
        :param end:
        :return:
        """
        return RedisWrapper.r.getrange(key, start, end)

    @staticmethod
    def get(key):
        if isinstance(key, list):
            return RedisWrapper.r.mget(key)
        else:
            return RedisWrapper.r.get(key)

    @staticmethod
    def remove(key):
        return RedisWrapper.r.delete(key)

    @staticmethod
    def incr(key, default=1):
        """
        自增
        :param key:
        :param default:
        :return:
        """
        return RedisWrapper.r.incr(key) if default == 1 else RedisWrapper.r.incr(key, default)

    # 自减
    @staticmethod
    def decr(key, default=1):
        """
        自减
        :param key:
        :param default:
        :return:
        """
        return RedisWrapper.r.decr(key) if default == 1 else RedisWrapper.r.decr(key, default)

    @staticmethod
    def hdel(name, key=None):
        """
        删除hashes
        :param name:
        :param key:
        :return:
        """
        if key:
            return RedisWrapper.r.hdel(name, key)
        return RedisWrapper.r.hdel(name)

    @staticmethod
    def clear():
        """
        清空当前db
        :return:
        """
        return RedisWrapper.r.flushdb()

    @staticmethod
    def lpush(key, value):
        """
        在 key 对应 list 的头部添加字符串元素
        :param key:
        :param value:
        :return:
        """
        return RedisWrapper.r.lpush(key, value)

    @staticmethod
    def lpop(key):
        """
        从 list 的尾部删除元素,并返回删除元素
        :param key:
        :return:
        """
        return RedisWrapper.r.lpop(key)
    @staticmethod
    def lpos(key,value):
        print("进入lpos")
        """
        从 list 的尾部删除元素,并返回删除元素
        :param key:
        :return:
        """
        return RedisWrapper.r.lpos(key,value)

    @staticmethod
    def sadd(key, value):
        """
        从 set 的添加一个成员
        :param key:
        :param value:
        :return:
        """
        return RedisWrapper.r.sadd(key, value)

    @staticmethod
    def sismember(key, member):
        """
        判断member元素是否是集合key的成员
        :param key:
        :param member:
        :return:
        """
        return RedisWrapper.r.sismember(key, member)

    @staticmethod
    def smembers(key):
        """
        获取 set 所有成员
        """
        return RedisWrapper.r.smembers(key)

    @staticmethod
    def srem(key, member):
        """
        获取 set 所有成员
        """
        return RedisWrapper.r.srem(key, member)

    @staticmethod
    def acquire(lock_key,value):
        """
        获取锁
        """
        if RedisWrapper.r.setnx(lock_key,value):
            # 设置锁的超时时间，防止进程崩溃导致锁无法释放
            RedisWrapper.r.expire(lock_key, 60)
            return True
        else:
            return False

    @staticmethod
    def rpush(key, value):
        return RedisWrapper.r.rpush(key, value)

    @staticmethod
    def lrange(key, start, end):
        return RedisWrapper.r.lrange(key, start, end)

    @staticmethod
    def llen(key):
        return RedisWrapper.r.llen(key)

    @staticmethod
    def lpop(key):
        return RedisWrapper.r.lpop(key)
class MyRedis(object):
    """
    连接 Redis
    """

    def __init__(self, host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DATABASE, password=REDIS_PASSWORD):
        self.r = redis.Redis(host=host, port=port, db=db, password=password)
